package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.BaseTrademark;
import com.atguigu.gmall.product.service.BaseTradeMarkService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("admin/product/baseTrademark")
public class TradeMarkController {

    @Autowired
    private BaseTradeMarkService baseTradeMarkService;

    //获取品牌分页列表
    //http://api.gmall.com/admin/product/baseTrademark/{page}/{limit}
    @GetMapping("{page}/{limit}")
    public Result pageTradeMark(@PathVariable Long page,
                                @PathVariable Long limit){

        Page<BaseTrademark> objectpage = new Page<>(page, limit);
        IPage baseTradeMarekList = baseTradeMarkService.getBaseTradeMarekList(objectpage);

        return Result.ok(baseTradeMarekList);
    }

    //获取品牌数据
    //  http://api.gmall.com/admin/product/baseTrademark/getTrademarkList
    @GetMapping("getTrademarkList")
    public Result getTrademarkList(){
        return Result.ok(baseTradeMarkService.list(null));
    }


    //添加品牌
    //http://api.gmall.com/admin/product/baseTrademark/save
    //接收前台传递过来的json字符串,使用@RequestBody转换为java对象
    @PostMapping("save")
    public Result saveTradeMark(@RequestBody BaseTrademark baseTrademark){
        boolean boo = baseTradeMarkService.saveTradeMark(baseTrademark);

        return Result.ok();
    }

    //修改品牌
    //http://api.gmall.com/admin/product/baseTrademark/update
    @PutMapping("update")
    public Result updateTrademark(@RequestBody BaseTrademark baseTrademark){
        baseTradeMarkService.updateById(baseTrademark);
        return Result.ok();
    }

    //删除品牌
    //http://api.gmall.com/admin/product/baseTrademark/remove/{id}
    @DeleteMapping("remove/{id}")
    public Result deleteTradeMark(@PathVariable Long id){
        boolean b = baseTradeMarkService.deleteTradeMark(id);
        return Result.ok();
    }

    //根据id获取品牌
    //http://api.gmall.com/admin/product/baseTrademark/get/{id}
    @GetMapping("get/{id}")
    public Result selectTradeMark(@PathVariable Long id){
        BaseTrademark baseTrademark = baseTradeMarkService.getById(id);
        return Result.ok(baseTrademark);
    }
}
